{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ef8e53dd-620c-468d-8b7b-4ce41681d4cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3ed30390-49ad-4ab5-84a3-a4f19613b120",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1=np.arange(10)\n",
    "print(arr1)\n",
    "type(arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c52cbffc-11fd-4125-a207-ec20011e5d5a",
   "metadata": {},
   "source": [
    "类型是n维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "28d4b1ad-beeb-494c-ba53-6a7c384b56c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape\n",
    "print(arr1.shape)\n",
    "arr1.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a13ccc99-6ff9-4644-9db3-aec63d324923",
   "metadata": {},
   "source": [
    "shape得到（10，），表示只有一个维度，shape[0],从0开始，取第一个维度元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e335aea0-7e4e-4eff-9723-e825439f3c20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n"
     ]
    }
   ],
   "source": [
    "my_list=[[1,2,3],[4,5,6],[7,8,9]]\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f0c7fa6d-7de9-4d31-b7b6-efb1b274d62d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2=np.array(my_list)\n",
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "aa911e99-968d-4dd3-804b-593489a3ce23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "218a15f3-02f6-401f-82a3-b982df02d55f",
   "metadata": {},
   "source": [
    "通过array将2维列表转成Numpy可处理的2darray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8d6dc17b-9380-42a3-9018-bda2139e903a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3]]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3=np.array([1,2,3],ndmin=3)\n",
    "arr3.shape\n",
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eec7c1d3-85ba-4295-85d1-edf7fd1c407e",
   "metadata": {},
   "source": [
    "ndmin将1维数组转成1个1行3列的3维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "42f6e582-0155-40dc-88ff-dd45cac20ea8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3.]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4=np.array([1,2,3],ndmin=2,dtype=float)\n",
    "arr4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bfe072e-db12-49ea-9409-4a8d77249695",
   "metadata": {},
   "source": [
    "dtype将数据类型转换成浮点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "01cfdeef-46d0-42c6-a376-52e4ae374c2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5=np.arange(10.1)\n",
    "arr5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d58322a7-fcb2-469f-8898-ef23c58c3a89",
   "metadata": {},
   "source": [
    "数据类型默认输入类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6a4fd78a-03fa-47c8-a602-ecdb8fa8293e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 3., 5., 7., 9.])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr6=np.arange(1,10,2,dtype=float)\n",
    "arr6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0269aa51-4143-4017-af8b-3e2bfd092b5e",
   "metadata": {},
   "source": [
    "起始，中止，间隔步长，数据类型4个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9c9fe415-6cd1-4a9b-866d-a6adc9ebae7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7=np.array([np.arange(1,3),np.arange(4,6),np.arange(7,9)])\n",
    "arr7.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "65983e22-1582-4d4c-965b-d567b0edb57c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [4, 5],\n",
       "       [7, 8]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2356725-c098-4bb0-a028-7082777b5337",
   "metadata": {},
   "source": [
    "用arange形成1维数组，然后array拼成3，2数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4802269f-09e6-426a-be3e-9e713256e966",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.93645379, 0.12568747, 0.10961477, 0.4333472 , 0.18422654,\n",
       "       0.72112825, 0.21700176, 0.37407083, 0.19682262, 0.22353283])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr8=np.random.random(size=10)\n",
    "arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e60345b2-c6c9-4968-9572-6b7fb107b54a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.75971146, 0.73580633, 0.47215272, 0.35487044],\n",
       "       [0.23994082, 0.72786255, 0.95941472, 0.39609508],\n",
       "       [0.8897696 , 0.51974969, 0.75750574, 0.33197416],\n",
       "       [0.24067308, 0.44140164, 0.85957977, 0.93865864]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr9=np.random.random(size=(4,4))\n",
    "arr9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44676a0a-7c5d-4f6c-b7b9-d70e8383764c",
   "metadata": {},
   "source": [
    "random生成0-1间随机数，左闭右开，size控制数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a1f21d2e-546c-49be-ac15-d2803625e76e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 4 7 7]\n",
      " [5 7 5 8]\n",
      " [7 7 8 6]\n",
      " [6 7 8 8]]\n"
     ]
    }
   ],
   "source": [
    "arr10=np.random.randint(10,size=10)\n",
    "arr10\n",
    "arr11=np.random.randint(4,9,size=(4,4))\n",
    "print(arr11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2976bea3-3b2e-4dcc-b8b9-f9e35fe16a97",
   "metadata": {},
   "source": [
    "randint(下界，上界，维度）生成整数数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "8372ef83-33b7-4999-afe4-43d8f0962775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-1.23044887,  0.01499764, -0.0194545 , -1.46114067],\n",
       "        [-0.04156642, -0.30229636, -0.25581312, -0.32404984],\n",
       "        [ 0.24858013,  0.54084271,  0.82783478, -1.17547723]],\n",
       "\n",
       "       [[ 0.31755199,  1.64400158,  0.56035408,  0.5700223 ],\n",
       "        [ 0.0965198 , -1.50385542,  0.39911292, -0.75765223],\n",
       "        [ 1.36323585,  0.94506139,  0.91027878, -1.2488329 ]]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr11=np.random.randn(2)\n",
    "arr11\n",
    "arr12=np.random.randn(2,3,4)\n",
    "arr12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e91157-d170-4620-8f0b-298286f503b8",
   "metadata": {},
   "source": [
    "arr11=np.random.randn(2,3,4)生成2个3行4列的3维数组，randn表示符合标准正态分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "33df39bc-b3ad-4cc7-a9e6-94c9e3ed2f5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.84770066,  1.17420251,  7.59399205,  8.35450111],\n",
       "       [-2.70143417, 13.59959373,  6.97578878,  8.53396391]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr12=np.random.normal(4,5,(2,4))\n",
    "arr12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0c1bc4a-b722-4a71-a8a0-9584b205317b",
   "metadata": {},
   "source": [
    "arr12=np.random.normal(4,5,(2,4))  期望4  方差5  2行4列的正态分布数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "803fd362-ce2f-4364-a1ba-692156fecb16",
   "metadata": {},
   "source": [
    "np.random.seed(10)，表示把随机种子固定为10，每次生成的都一样，方便同数据测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9cf4ed4a-6ea6-4cce-a76b-30f31be6ba04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7 6 5]\n",
      " [2 4 5]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[7, 6, 5],\n",
       "       [2, 4, 5]], dtype=int32)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr13=np.random.randint(2,9,(2,3))\n",
    "arr13\n",
    "print(arr13)\n",
    "np.random.shuffle(arr13)\n",
    "arr13"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8be9ac15-22c3-4ddd-9f72-82a8bf3914a4",
   "metadata": {},
   "source": [
    "np.random.shuffle(arr13)，重新排列数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3e48be2f-6e56-47cb-8e6f-6fa75e79c868",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2   (2, 3)   6   4   int32\n"
     ]
    }
   ],
   "source": [
    "print(arr13.ndim,\" \",arr13.shape,\" \",arr13.size,\" \",arr13.itemsize,\" \",arr13.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2591daa4-a5ab-4e01-a5ad-9b1a9629362a",
   "metadata": {},
   "source": [
    "打印数组的维度，形状，元素个数，单个元素大小，数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "001f0d4e-0f0f-40af-ba8c-34d1f123608b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "arr14=np.zeros((2,3),float,'C')\n",
    "print(arr14)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1841860b-4761-4d8b-8afd-71059b3aa03e",
   "metadata": {},
   "source": [
    "arr14=np.zeros((2,3),float,'C')   初始化全0  形状2*3  浮点  行优先"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "28c81cf2-9f9b-4182-8392-3a42256a4443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr15=np.ones((2,3))\n",
    "arr15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "77d87036-b87d-449c-b578-0b778f7d0d7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr16=np.empty((2,3))\n",
    "arr16"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e62698ce-bab2-4e8a-b844-6cde8ae7f01f",
   "metadata": {},
   "source": [
    "arr16=np.empty((2,3))   创建数组时不初始化，沿用内存脏数据，速度快，适合后续重新赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "17a4ef00-d452-4350-bfdb-581962992c10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  6,  9, 12])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr17=np.linspace(1,12,5,1,0,int)\n",
    "arr17"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8042eff5-69d6-4085-8f00-8a0814c76f23",
   "metadata": {},
   "source": [
    "arr17=np.linspace(1,12,5,1,0,int)  1维等差数列  起点  终点  是否包含终点  间距  数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "b26aa18e-b320-4cce-b37c-fdc67dbd96fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0. , 2.5, 5. , 7.5]), np.float64(2.5))"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr18=np.linspace(0,10,4,0,1)\n",
    "arr18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "1f098bfc-34de-45a7-8c3c-45c0f8a55a06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr19=np.logspace(0,9,10,base=2)\n",
    "arr19"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c1e7780-d9a0-4a9a-8ec7-55834cea4071",
   "metadata": {},
   "source": [
    "生成1维等比数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "36af38fc-4c2f-4a50-979d-d9c914b0ce47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[0 0 0]\n",
      " [0 0 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list=([1,2,3],[4,5,6])\n",
    "arr20=np.array(list)\n",
    "print(arr20)\n",
    "arr21=np.zeros_like(arr20)\n",
    "print(arr21)\n",
    "arr21=np.empty_like(arr20)\n",
    "arr21"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be1b2fc6-6234-43d5-9542-12632672d3d9",
   "metadata": {},
   "source": [
    "zeros_like（arr）表示创建和arr形态一样的数组，仅仅值全0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "db327a07-5e02-4996-a4e7-cb681b5e7751",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(7.38905609893065)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f7efacc-6823-4cee-850b-f905c8323a99",
   "metadata": {},
   "source": [
    "算e的2次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a981fad-66cd-4e0b-bd17-95f8b1063e4a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
